<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 视频网课管理
 */
class Course_zhibo extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

		$this->school_id = $this->user_data['school_id'];
		$this->teacher_id = $this->user_data['teacher_id'];
		$this->uid = $this->user_data['id'];
		$this->load->model('videos');

		$parent_id = 3;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "课程管理", "curr" => '/course_zhibo', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", "course");

	}


	/**
	 * 直播list
	 * @param  integer $status  [description]
	 * @param  integer $sort    [description]
	 * @param  integer $asc_des [description]
	 * @param  integer $search  [description]
	 * @param  integer $kemu    [description]
	 * @param  integer $limit   [description]
	 * @return [type]           [description]
	 */
	function index($status = 10,$sort = 0,$asc_des = 0,$search = 0, $cate_id=0, $kemu=0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$review = isset($post_data['status']) ? $post_data['status'] : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		$this->load->model("Courses");
		$data = array( 'status' => $status, 'cate_id' => $cate_id, 'kemu'=>$kemu, 'type' => 2, 'sort' => $sort,'asc_des' => $asc_des,'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '直播课列表');
		// $offset = 1;
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->model('Anchors');
		$result = $this->Anchors->get_zhibo_list_ofpage($data['limit'], $this->offset,$data);
		if(isset($result['result'])){
			foreach ($result['result'] as $key => $value) {
				$time = time();
				if($value['start_time'] < $time && $time < $value['end_time']){
					$result['result'][$key]['live_status'] = 2;
				}
				if($value['start_time'] > $time){
					$result['result'][$key]['live_status'] = 1;
				}
				if($value['end_time'] < $time){
					$result['result'][$key]['live_status'] = 3;
				}
			}
		}

		//获取app 活动

		/*$this->load->model('activitiess');
		$activity_list = $this->activitiess->get_list(0, 30,  array('type'=>2));*/
		$subject = $this->Courses->get_course_cate_subject($cate_id);
		$cate = $this->Courses->get_cate(3);
		$this->load->library('pagination');
		$page_config['base_url'] = '/course_zhibo/index/'.$data['status'].'/'.$data['sort'].'/'.$data['asc_des'].'/'.$data['search'].'/'.$data['cate_id'].'/'.$data['kemu'];
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("wap_url", $this->config->item('wap_url'));
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("zhiboke", $result['result']);
		// $this->smarty->assign("activity_list", $activity_list);

		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("subject", $subject);
		$this->smarty->display("course/course_zhiboke_list.html");
	}

	public function edit($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->load->model('Anchors');
			$this->load->model("Courses");
			$token = get_qiniu_token('VIMG');
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$post_data['start_time'] = strtotime($post_data['start_time']);
				$post_data['end_time'] = strtotime($post_data['end_time']);
				
				if(isset($post_data['img_url'])){
					$image = $post_data['img_url'];
					if(!empty($image)){
						$post_data['course_img'] = get_qiniu_url($image[0]);
						unset($image[0]);
						if(!empty($image)){
							$post_data['course_imgs'] = json_encode($image);
						}
					}
					unset($post_data['img_url']);
				}
				if(isset($post_data['file'])) unset($post_data['file']);
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course',$post_data);
			}
			$result = $this->Anchors->get_zhiboke_info($id);
			$subject = $this->Courses->get_subject();
			$teacher = $this->Courses->get_teacher($this->user_data['school_id']);
			$img_url = json_decode($result['course_imgs']);
			$cate = $this->Courses->get_cate(3);
			$this->smarty->assign("cate", $cate);
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("menu", 'course');
			$this->smarty->assign("teacher", $teacher);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("img_url", $img_url);
			$this->smarty->assign("token", $token);
			//$this->smarty->assign("post_url", "/course/edit_zhiboke");
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
			$this->smarty->display("course/add_zhibo_course.html");

		}
	}

	function delete($id = 0){
		if(empty($id)){
			show_message("/course_zhibo", "非法操作");
		}else{
			delete_data((array)$id, "course");
			show_message("/course_zhibo");
		}
	}

	/**
	 * 发布直播
	 * @return [type] [description]
	 */
	public function publish(){
		$token = get_qiniu_token('VIMG');
		$post_data = $this->input->post();
		if($post_data){

			if($post_data['title'] == ""){
				echo  "标题不能为空";exit;
			}
			$post_data['school_id'] = $this->user_data['school_id'];
			$post_data['teacher_id'] = $this->user_data['teacher_id'];
			$post_data['add_time'] = time();
			$post_data['type'] = 2;
			$post_data['start_time'] = strtotime($post_data['start_time']);
			$post_data['end_time'] = strtotime($post_data['end_time']);
			for ($i=0; $i < count($post_data['img_url']); $i++) {
				$post_data['img_url'][$i] = get_qiniu_url($post_data['img_url'][$i]);
			}
			$image = $post_data['img_url'];
			if(!empty($image)){
				$post_data['course_img'] = $image[0];
				unset($image[0]);
				if(!empty($image)){
					$post_data['course_imgs'] = json_encode($image);
				}
			}
			unset($post_data['img_url']);
			unset($post_data['id']);
			unset($post_data['file']);
			$result = $this->db->insert('course',$post_data);
			if($result){

			}
		}
		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher($this->user_data['school_id']);
		$cate = $this->Courses->get_cate(3);
		$this->smarty->assign("cate", $cate);

		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("token", $token);
		$this->smarty->assign("post_url", "/course_zhibo/publish");
		$this->smarty->display("course/add_zhibo_course.html");
	}


	/**
	 * 课程分类
	 * @param  integer $search [description]
	 * @param  integer $limit  [description]
	 * @return [type]          [description]
	 */
	function cate_list($search = 0,$limit = 0){
		$offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 5;
		}
		$this->load->model("Courses");
		$list = $this->Courses->get_cate_list($search,3,$offset,$limit);
		$data = array( 'search' => $search,'limit' => $limit);
		$this->smarty->assign("action", '直播课分类列表');

		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		$page_config['base_url'] = '/course/cate_list/'.$data['search'].'/';
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("current_url", $current_url);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("data", $data);

		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course_zhibo/cate_list.html");
	}



	function cate_publish(){
		$post_data = $this->input->post();
		if($post_data){
			unset($post_data['id']);
			if($post_data['name'] == ""){
				echo  "标题不能为空";exit;
			}
			if($post_data['sort'] == ""){
				echo  "排序不能为空";exit;
			}
			$post_data['type'] = 3;
			$post_data['school_id'] = $this->school_id;
			$post_data['teacher_id'] = $this->teacher_id;
			$post_data['add_time'] = time();
			$result = $this->db->insert('course_cate',$post_data);
			if($result){
				redirect("/course_zhibo/cate_list");
			}
		}
		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher($this->user_data['school_id']);

		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$this->smarty->display("course_zhibo/add_course_cate.html");
	}


	public function edit_course_cate($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->load->model("Courses");
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course_cate',$post_data);
				if($result){
					redirect("/course_zhibo/cate_list");
				}
			}
			$result = $this->Courses->get_cate_info($id);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

			$this->smarty->display("course_zhibo/add_course_cate.html");

		}
	}


	function delete_course_cate($id){
		if($id > 0){
			$this->db->where('id',$id);
			$result = $this->db->delete('course_cate');
			if($result){
				redirect("/course_zhibo/cate_list");
			}
		}

	}

	/**
	 * 网课科目list
	 * @param  integer $search [description]
	 * @param  integer $type   [description]
	 * @param  integer $limit  [description]
	 * @return [type]          [description]
	 */
	function cate_subject_list($search = 0,$cate = 0,$limit = 0){
		$offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$cate = $post_data['cate'] ? $post_data['cate'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 5;
		}
		$this->load->model("Courses");
		//取视频课科目
		$list = $this->Courses->get_cate_subject_list($search,$cate,3,$offset,$limit);

		//获取视频课分类
		$cate_list = $this->Courses->get_cate(3);//默认取直播的分类
		$this->smarty->assign('cate_list',$cate_list);


		$data = array( 'search' => $search,'cate'=>$cate,'limit' => $limit);
		$this->smarty->assign("action", '视频课科目列表');

		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		$page_config['base_url'] = '/course_zhibo/cate_subject_list/'.$data['search'].'/'.$data['cate'].'/';
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("current_url", $current_url);

		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("data", $data);

		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course_zhibo/course_cate_subject_list.html");
	}

	/**
	 * 发布网课的科目
	 * @return [type] [description]
	 */
	function cate_subject_publish(){
		$post_data = $this->input->post();

		if($post_data){
			unset($post_data['id']);
			if($post_data['name'] == ""){
				echo  "标题不能为空";exit;
			}
			if($post_data['sort'] == ""){
				echo  "排序不能为空";exit;
			}
			$post_data['type'] = 3;
			$post_data['school_id'] = $this->school_id;
			$post_data['teacher_id'] = $this->teacher_id;
			$post_data['add_time'] = time();
			$result = $this->db->insert('course_cate_subject',$post_data);
			if($result){
				redirect("/course_zhibo/cate_subject_list");
			}
		}

		$this->load->model("Courses");
		$subject = $this->Courses->get_subject();
		$teacher = $this->Courses->get_teacher();

		$cate = $this->Courses->get_cate(3);//默认取直播的分类


		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("teacher", $teacher);
		$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$this->smarty->display("course_zhibo/add_course_cate_subject.html");
	}


	/**
	 * 编辑网课科目
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function edit_course_cate_subject($id = 0){
		$id = intval($id);
		if($id > 0){
			$this->load->model("Courses");
			$post_data = $this->input->post();
			if(isset($post_data['id']) && $post_data['id'] > 0){
				$this->db->where('id', $post_data['id']);
				$result = $this->db->update('course_cate_subject',$post_data);
				if($result){
					redirect("/course_zhibo/cate_subject_list");
				}
			}
			$result = $this->Courses->get_cate_subject_info($id);


			$cate = $this->Courses->get_cate($result['type']);

			$this->smarty->assign("cate", $cate);
			$this->smarty->assign("info", $result);
			$this->smarty->assign("post_url", 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

			$this->smarty->display("course_zhibo/add_course_cate_subject.html");

		}
	}

	function delete_course_cate_subject($id){
		if($id > 0){
			$this->db->where('id',$id);
			$result = $this->db->delete('course_cate_subject');
			if($result){
				redirect("/course_zhibo/cate_subject_list");
			}
		}

	}

}
